<?php
// namespace Controller;

/**
 * Controller 控制类
 * 描述：接收过滤对象传的参数，然后执行对应操作
 */
class Controller{
	var $host_address = "localhost";//文件url需要的服务地址
	function __construct($role,$action){
		// $this->getRelParams();
		switch($role){
			case'Customer':
				$this->cusSwitch($action);
				break;
			case'Manager':
				$this->manSwitch($action);
				break;
			case'Business':
				$this->busSwitch($action);
				break;
			default:
				break;
		}
	}
	function relParamsDetecte(){
		//嗅探依赖参数
		//全部都存在则返回参数数组
		//有不存在的则输出缺失，返回false
		//在这个过程中会对参数进行sql注入检测
		include_once("./Security.php");
	    $args = func_get_args();
	    $missList = array();
	    $results = array();
	    foreach ($args as $arg) {	//缺失检测
	        if(isset($_POST[$arg])==false){
	            array_push($missList,$arg);
	        }
	    }
	    if(empty($missList)){		//没有缺失就返回结果
	    	foreach($args as $arg){ //sql注入检测
	    		if(detectSqlInjection($_POST[$arg])==true){
	    			die('Injection Detected!');
	    			return false;
	    		}
	    		$results[$arg] = $_POST[$arg];
	    	}
	        return $results;
	    }else{						//如果有缺失就输出缺失
	        $missString = '';
	        foreach ($missList as $value){
	            $missString = $missString." ".$value." |";
	        }
	        echo "Action's releted params Miss:".$missString;
	        return false;
	    }
	}

	function cusSwitch($action){
		include_once('./Model/Customer.php');
		$cus = new Cus;
		switch($action){
			case'getPageConfigData':
				$cus->getPageConfigData();
				break;
			case'login':
				$cus->login($this->relParamsDetecte('code'),$this->host_address);
				break;
			case'autoLogin':
				$cus->autologin($this->relParamsDetecte('openid'));
				break;
			case'checkGoods':
				$cus->checkGoods($this->relParamsDetecte('pageNo','typeIndex'));
				break;
			case'getGoodDetail':
				$cus->getGoodDetail($this->relParamsDetecte('gid'));
				break;
			case'addShopCart':
				$cus->addShopCart($this->relParamsDetecte('cid','gid','num','priceIndex'));
				break;
			case'checkShopCart':
				$cus->checkShopCart($this->relParamsDetecte('cid'));
				break;
			case'deleteShopCart':
				$cus->deleteShopCart($this->relParamsDetecte('cid','gidList'));
				break;
			case'addOrder':
				$cus->addOrder($this->relParamsDetecte('orderList'));
				break;
			case 'checkOrder':
				$cus->checkOrder($this->relParamsDetecte('cid'));
				break;
			case 'comfirmOrder':
				$cus->comfirmOrder($this->relParamsDetecte('cid','oid')); 
				break;
			case'addComment':
				$cus->addComment($this->relParamsDetecte('cid','gid','words'));
				break;
			case'checkComment':
				$cus->checkComment($this->relParamsDetecte('gid'));
				break;
			case'insertHimage':
				$cus->insertHimage($this->relParamsDetecte('cid'),$this->host_address);
				break;
			case'alterClientInfo':
				$cus->alterClientInfo($this->relParamsDetecte('submitInfo'));
				break;
			case'checkBasicInfo':
				$cus->checkBasicInfo($this->relParamsDetecte('cid'));
				break;
			case 'searchGoods':
				$cus->searchGoods($this->relParamsDetecte('keyword','page','pageLength'));
				break;
			default:
				break;
		}
	}

	function busSwitch($action){
		include_once('./Model/Business.php');
		$bus = new Bus;
		switch($action){
			case'login':
				$bus->login($this->relParamsDetecte('account','password'));
				break;
			case'registe':
				$bus->registe($this->relParamsDetecte('account','password'));
				break;
			case'checkGoods':
				$bus->checkGoods($this->relParamsDetecte('bid'));
				break;
			case'addGood':
				$bus->addGood($this->relParamsDetecte('bid','gtype','name','type','specifications','price','acttime','author','description'),$this->host_address);
				break;
			case'deleteGood':
				$bus->deleteGood($this->relParamsDetecte('bid','gidList'));
				break;
			case'checkOrders':
				$bus->checkOrders($this->relParamsDetecte('bid'));
				break;
			case'dealOrder':
				$bus->dealOrder($this->relParamsDetecte('bid','oid'));
				break;
			default:
				break;
		}
	}

	function manSwitch($action){
		include_once('./Model/Manager.php');
		$man = new Man;
		switch($action){
			case'login':
				$man->login($this->relParamsDetecte('account','password'));
				break;
			case'registe':
				$man->registe($this->relParamsDetecte('account','password'));
				break;				
			default:
				break;
		}		
	}
}

?>